package cn.edu.ctbu.datajpa01.service.impl;

import cn.edu.ctbu.datajpa01.domain.Banji;

import cn.edu.ctbu.datajpa01.domain.repository.BanjiRepository;

import cn.edu.ctbu.datajpa01.service.BanjiService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BanjiImpIService implements BanjiService {

    @Autowired
    private BanjiRepository banjiRepository;

    public List<Banji> findAll(){

        return banjiRepository.findAll();
    }

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Banji> findAll(Pageable pageable){

        return banjiRepository.findAll(pageable);

    }

    public Page<Banji> findAll(Example<Banji> banji, Pageable pageable){

        return banjiRepository.findAll(banji,pageable);

    }

    //按id进行查询 @param id 主键，整数
    //@return
    public Banji findById(Integer id){

        return banjiRepository.findById(id).orElse( null);

    }

    public Banji getById(Integer id){

        Banji banji=banjiRepository.findById(id).orElse(null);
        return banji;
    }

    public Banji insert(Banji banji){

        banjiRepository.save(banji);
        return banji;
    }

    public Banji update(Banji banji){

        banjiRepository.save(banji);
        return banji;
    }

    public void delete(Banji banji){

        banjiRepository.delete(banji);

    }

    public void delete(Integer id){

        Banji banji=new Banji();
        banji.setId(id);
        banjiRepository.delete(banji);

    }

}
